﻿using MapHello.Basic;
using MapHello.Data;
using MapHello.JSON;
using Microsoft.Live;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace MapHello.Service.Live
{
    class LiveService
    {
        public static readonly string[] Scopes = new string[] { "wl.basic" };

        public static async Task<Person> LoadPerson(string token, string personId = "")
        {
            string httpStr = "";
            string imageRequest = "";

            if (string.IsNullOrEmpty(personId))
            {
                httpStr = string.Format("{0}/me?access_token={1}", Constants.WindowsLiveServicePrefix, token);
                imageRequest = string.Format("{0}/me/picture?access_token={1}", Constants.WindowsLiveServicePrefix, token);
            }
            else
            {
                httpStr = string.Format("{0}/{1}?access_token={2}", Constants.WindowsLiveServicePrefix, personId, token);
                imageRequest = string.Format("{0}/{1}/picture?access_token={2}", Constants.WindowsLiveServicePrefix, personId, token);
            }

            var request = await Utility.ProcessHttpRequest(httpStr);

            DataContractJsonSerializer s = new DataContractJsonSerializer(typeof(Profile));
            Profile me = s.ReadObject(request.GetResponseStream()) as Profile;

            var image = await Utility.LoadImage(imageRequest);


            return new Person()
            {
                Id = me.Id,
                Name = me.Name,
                ImageUrl = image.Key,
                ImageBinary = image.Value
            };
        }


    }
}
